<?php

/**
 * 初始化 wiki basic 数据。
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\service\wiki\WikiBasic as sWikiBasic;

$page = 1;
$pageSize = 2000;
$list = [];
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $list = mWiki::select('*', [
        'basic_id' => 0,
        'LIMIT' => $limit
    ]);
    foreach ($list as $row) {
        $wikiBasicData = [
            'brand_id' => $row['brand_id'],
            'title' => $row['title'],
            'category_id' => $row['category_id'],
            'second_category_id' => $row['second_category_id'],
            'third_category_id' => $row['third_category_id'],
            'four_category_id' => $row['four_category_id'],
            'last_category_id' => $row['last_category_id'],
            'basic_state' => mWikiBasic::STATE_NORMAL,
            'addtime' => $row['addtime'],
            'edittime' => $row['addtime'],
        ];
        $basicId = mWikiBasic::insertData($wikiBasicData);
        if ($basicId) {
            mWiki::updateData(['basic_id' => $basicId, 'source_basic_id' => $basicId, 'is_default' => 1], ['id' => $row['id']]);
            var_dump('更新 id:' . $row['id'] . '的basic_id:' . $basicId . '#sql:' . json_encode(mWiki::log()));
            var_dump('更新 basic id:' . $basicId . '#brand_id:' . $wikiBasicData['brand_id']);
            sWikiBasic::afterEditWiki([$basicId], $wikiBasicData['brand_id']);
            sWikiBasic::changeBrandBasicCount($wikiBasicData['brand_id']);
        }
    }
    $page++;
} while ($list);


//更新wikibasic 数据 及 afterEditWiki
//$page = 1;
//$pageSize = 2000;
//$basicList = [];
//do {
//    $limit = [($page - 1) * $pageSize, $pageSize];
//    $basicList = mWikiBasic::select('*', ['LIMIT' => $limit]);
//    foreach ($basicList as $row) {
//        var_dump('更新 basic id:' . $row['id'] . '#brand_id:' . $row['brand_id']);
//        if ($row['brand_id'] == 0) {
//            $spuInfo = mWiki::getByWhere(['basic_id' => $row['id'], 'brand_id[>]' => 0]);
//            mWikiBasic::updateData(['brand_id' => $spuInfo['brand_id']], ['id' => $row['id']]);
//        }
//        sWikiBasic::afterEditWiki([$row['id']], $row['brand_id']);
//        sWikiBasic::changeBrandBasicCount($row['brand_id']);
//    }
//    $page++;
//} while ($basicList);


//修复线上wiki表有brand_id ,wiki_basic 表没有brand_id , 只到正式上线后，修复一次，才完整
//wiki表数据，在shop-admin 线上后台，有人审核，添加了品牌。导致wiki_basic表brand_id 未更新
$page = 1;
$pageSize = 2000;
$spuList = [];
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $spuList = mWiki::selectByWhere(['LIMIT' => $limit, 'brand_id[>]' => 0, 'ORDER' => ['id' => 'DESC']]);
    var_dump('page:' . $page . 'count:' . count($spuList));
    foreach ($spuList as $row) {
        $spuCount = mWiki::selectCountWhere(['basic_id' => $row['basic_id']]);
        if ($spuCount == 1) {
            $basicInfo = mWikiBasic::getByWhere(['id' => $row['basic_id']]);
            if (!empty($basicInfo)) {
                $changeData = [];
                if ($basicInfo['brand_id'] == 0) {
                    $changeData['brand_id'] = $row['brand_id'];
                    if ($basicInfo['last_category_id'] == 0) {
                        $changeData['category_id'] = $row['category_id'];
                        $changeData['second_category_id'] = $row['second_category_id'];
                        $changeData['third_category_id'] = $row['third_category_id'];
                        $changeData['four_category_id'] = $row['four_category_id'];
                        $changeData['last_category_id'] = $row['last_category_id'];
                    }
                }
                if ($changeData) {
                    var_dump('basic Id:' . $row['basic_id'] . '更新brand_id:' . $row['brand_id']);
                    mWikiBasic::updateData($changeData, ['id' => $row['basic_id']]);
                    var_dump(mWikiBasic::log());
                    sWikiBasic::afterEditWiki([$row['basic_id']], $row['brand_id']);
                    sWikiBasic::changeBrandBasicCount($row['brand_id']);
                }
            }
        }
    }
    $page++;
} while ($spuList);
